
******CO MEETING SELECTION INTO VERIFIED MPRS SAMPLE (TABLE C.1)********


set more off
clear all 
clear matrix
capture log close

adopath + "$main/DoFiles"																		


local seed = 9998
local reps = 5000


//--> CO MONTH SELECTION INTO VERIFIED MPRS

		use "$data/MPRS_comthlevel.dta", clear
		
		gen audit_co = (cro_att_==1)
		gen mprs_co = 1
		keep co_code month audit_co mprs_co
		tempfile temp_audit
		save `temp_audit'
		
		
		
//--> PRE BONUS MIS CO_LEVEL OUTCOMES

		use "$data/MIS_comthlevel.dta", clear
		
			g inv_ratio_miss_pay_20 = 1- (miss_pay_20/num_pay) 
			g inv_ratio_miss_pay_month = 1- (miss_pay_month/num_pay)

			
		keep if month<=10
		local var num_active_loan inv_ratio_miss_pay_20 num_new_loan amount_issue inv_ratio_miss_pay_month miss_pay_maturity_month
		collapse (mean) `var', by(co_code)
		
		sort co_code
		tempfile temp_predata
		save `temp_predata'
		
		
//--> 	CO TREATMENT ASSIGNMENT

		use "$data/FWCO_panel.dta"
		sort fw_code
		merge fw_code using "$data/FWHR.dta"
		drop _merge
		
		keep if month>=11
		
		//--> FEW COS w/ CHANGING FU TREATMENT STATUS--> ASSIGNMENT THE MODE TREATMENT/FU
		
					bys co_code: egen treat_co = mode(treat), maxmode
					bys co_code: egen treat_credit_co = mode(treat_credit), maxmode
					bys co_code: egen treat_sm_co = mode(treat_sm), maxmode

					bys co_code: egen fu_feb05_co = mode(fu_feb05), maxmode
					bys co_code: egen region_co = mode(region), maxmode			
		
		
		keep co_code treat_co treat_credit_co treat_sm_co fu_feb05_co region_co
		bys co_code: keep if _n==1
		sort co_code 
		tempfile temp_treat
		save `temp_treat'
		
		
//--> COMBINE DATA

		use	`temp_audit'
		sort co_code 
		merge co_code using `temp_treat', nok
		drop _m
		sort co_code
		merge co_code using `temp_predata', nok
		drop _merge
	
		//--> INTERACTION VARS
		
				g inter_credit_audit_co = audit_co*treat_credit_co
				g inter_sm_audit_co = audit_co*treat_sm_co
				
		
		//--> CALCULATE INDEX
		
		
			replace inv_ratio_miss_pay_20 = 1 if inv_ratio_miss_pay_20==.				//--> NO DEFAULTS (DUE TO NO DUES)
			replace inv_ratio_miss_pay_20 = . if num_active_loan==.
			
			replace inv_ratio_miss_pay_month = 1 if inv_ratio_miss_pay_month==.
			replace inv_ratio_miss_pay_month = . if num_active_loan==.
		
			
			local var num_active_loan inv_ratio_miss_pay_20 num_new_loan amount_issue inv_ratio_miss_pay_month miss_pay_maturity_month
			
					foreach m in `var' {								
						sum `m' if treat_co==0, d
						local mean = r(mean)
						local sd = r(sd)						
						gen std`m' = (`m' - `mean')/`sd'
						}	
					
						
						//--> INVERSE THE SCALE OF NUMBER OF LOANS PAST MATURITY
							
								foreach x in miss_pay_maturity_month {
									replace std`x' = -1*std`x'
									}
									
					egen co_index = rowmean(stdnum_active_loan stdinv_ratio_miss_pay_20 stdnum_new_loan stdamount_issue stdinv_ratio_miss_pay_month stdmiss_pay_maturity_month)
					egen stdco_index= std(co_index)
										
					
			
//--> OUTPUT: ONLINE APPENDIX TABLE C.1 	
	*********************************************************************************************************************************************************
			
	local pre_var num_active_loan inv_ratio_miss_pay_20 num_new_loan amount_issue inv_ratio_miss_pay_month miss_pay_maturity_month stdco_index
	
	tab month, gen(m)
	tab region, gen(r)

	
		//--> REGRESSION OUTPUT (FOR ESTIMATED COEEFICIENTS ONLY)
			****************************************		
	
			xi: reg audit_co i.region_co i.month treat_credit_co treat_sm_co
			sum audit_co if treat_co==0
			local m = r(mean)
			outreg2 using "$output/tableC_1 (coeff).xls", replace addstat("CONTROL MEAN", `m') noaster stats(coef) keep(treat_credit_co treat_sm_co) nocons nor2
			local replace
			foreach v in `pre_var'{
				xi: reg `v' i.region i.month audit_co treat_credit_co treat_sm_co inter_credit_audit_co inter_sm_audit_co
				sum `v' if treat_co==0 
				local m = r(mean)
				outreg2 using "$output/tableC_1 (coeff).xls", `replace' addstat("CONTROL MEAN", `m') noaster stats(coef) keep(treat_credit_co treat_sm_co inter_credit_audit_co inter_sm_audit_co audit_co) nocons nor2
				local replace
				}
		

		log using "$output/tableC_1 (p-values)", replace	
		
		//--> CALCULATE WILDCLUSTER T-BOOTSTRAP P VALUES
			****************************************		
			
			//--> CREDIT VS. CONTROL; SOCIAL VS. CONTROL
				
					cgmwildboot audit_co m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 r1 r2 r3 treat_credit_co treat_sm_co, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed') 
					foreach v in `pre_var' {
						cgmwildboot `v' m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 r1 r2 r3 audit_co treat_credit_co treat_sm_co inter_credit_audit_co inter_sm_audit_co, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
						}
									
				
			//--> CREDIT VS. SOCIAL
			
					g x1 = treat_credit_co + treat_sm_co
					g test1 = treat_credit_co //--> RELEVANT TEST COL.1
					
					
					g test5 = audit_co*treat_credit_co //--> RELEVANT TEST COLS. 2-8
					g x5_2 = inter_credit_audit_co + inter_sm_audit_co 
					
					cgmwildboot audit_co m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 r1 r2 r3 test1 x1, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
					foreach v in `pre_var' {
						cgmwildboot `v' m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 r1 r2 r3 test5 audit_co treat_credit_co treat_sm_co x5_2, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
						}

											
					
	//--> CALCULATE RANDOMIZATION INFERENCE BASED P VALUES
			****************************************						
						
			//--> CREDIT VS. CONTROL; SOCIAL VS. CONTROL	
			
			
				randcmd ((treat_credit_co treat_sm_co) reg audit_co m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 r1 r2 r3 treat_credit_co treat_sm_co, cluster(fu_feb05)), treatvars(treat_credit_co treat_sm_co) groupvar(fu_feb05) seed(`seed') reps(`reps')
				foreach v in `pre_var' {
					randcmd((audit_co treat_credit_co treat_sm_co inter_credit_audit_co inter_sm_audit_co) reg `v' m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 r1 r2 r3 audit_co treat_credit_co treat_sm_co inter_credit_audit_co inter_sm_audit_co, cluster(fu_feb05)), treatvars(treat_credit_co treat_sm_co) groupvar(fu_feb05) seed(`seed') reps(`reps') calc1(replace inter_credit_audit_co = audit_co*treat_credit_co) calc2 (replace inter_sm_audit_co = audit_co*treat_sm_co)
					}	
			
			//--> CREDIT VS. SOCIAL (F TEST)
			
				randcmd ((test1) reg audit_co m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 r1 r2 r3 test1 x1, cluster(fu_feb05)), treatvars(treat_credit_co treat_sm_co) groupvar(fu_feb05) seed(`seed') reps(`reps') calc1(replace test1 = treat_credit_co) calc2(replace x1 = treat_credit_co + treat_sm_co) 
				
				foreach v in `pre_var' {
					randcmd((test5) reg `v' m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 r1 r2 r3 test5 audit_co treat_credit_co treat_sm_co x5_2, cluster(fu_feb05)), treatvars(treat_credit_co treat_sm_co) groupvar(fu_feb05) seed(`seed') reps(`reps') calc1(replace test5 = audit_co*treat_credit_co) calc2 (replace inter_credit_audit = audit_co*treat_credit_co) calc3 (replace inter_sm_audit_co = audit_co*treat_sm_co) calc4 (replace x5_2 = inter_credit_audit_co + inter_sm_audit_co) 
					}	
					
	log close
	*********************************************************************************************************************************************************
						
	
